﻿using System;
using System.Collections;
using System.Configuration;
using System.Globalization;

namespace DMS.Integration
{

    public class ReceiptDocMetadata : IMetadata
    {
        private static string _receiptWeb = "Receipts";
        private static string _receiptDocumentLibrary = "ReceiptDocuments";

        public string Id { get; set; }
        public int Year { get; set; }
        //public string DocumentType { get; set; }
        public string CustomerName { get; set; }
        public string FirstName { get; set; }
        public string CustomerNumber { get; set; }
        public decimal TotalAmount { get; set; }
        public string BranchName { get; set; }
        public string Username { get; set; }
        public string InvoiceNumber { get; set; }
        public string InvoiceType { get; set; }
        public decimal Amount { get; set; }
        public string ReceiptType { get; set; }
        public DateTime StartTime { get; set; }
        public string BidderNo { get; set; }
        public string HistDocumentNo { get; set; }
        public DateTime ReceiptDate { get; set; }

        private string _siteCollectionUrl = ConfigurationManager.AppSettings["SharePointDMSSiteUrl"];
        public string SiteCollectionUrl
        {
            get { return _siteCollectionUrl; }
            set { _siteCollectionUrl = value; }
        }
        public string DocumentLink
        {
            get
            {
                if (!SiteCollectionUrl.EndsWith("/"))
                    SiteCollectionUrl += "/";
                return string.Format("{0}{1}/{2}/{3}/{4}/{5}.pdf, {3}.pdf", SiteCollectionUrl, _receiptWeb, Year, _receiptDocumentLibrary, GetFolderName(Id), Id);
            }
        }
        private string GetFolderName(string filename)
        {
            int suffixdot = filename.IndexOf(".", System.StringComparison.Ordinal);
            int separater = filename.LastIndexOf("-", StringComparison.CurrentCultureIgnoreCase);
            if (suffixdot < separater || suffixdot == -1)
            {
                if (separater != -1)
                    return filename.Substring(separater + 1);
            }
            string folderName = filename.Substring(separater + 1, suffixdot - separater - 1);
            return folderName;
        }

        //public int BranchId { get; set; }
        public Hashtable ToStringDictionary()
        {
            Hashtable dict = new Hashtable();
            dict.Add("Title", Id);
            if (!string.IsNullOrEmpty(CustomerName)) dict.Add("CustomerName", CustomerName);
            if (!string.IsNullOrEmpty(CustomerNumber)) dict.Add("CustomerNumber", CustomerNumber);
            if (!string.IsNullOrEmpty(HistDocumentNo)) dict.Add("HistDocumentNo", HistDocumentNo);
            if (!string.IsNullOrEmpty(InvoiceType)) dict.Add("InvoiceType", InvoiceType);
            if (!string.IsNullOrEmpty(FirstName)) dict.Add("FirstName", FirstName);
            if (!string.IsNullOrEmpty(BranchName)) dict.Add("BranchName", BranchName);
            if (!string.IsNullOrEmpty(Username)) dict.Add("Username", Username);
            if (!string.IsNullOrEmpty(InvoiceNumber)) dict.Add("InvoiceNumber", InvoiceNumber);
            if (!string.IsNullOrEmpty(ReceiptType)) dict.Add("ReceiptType", ReceiptType);
            if (!string.IsNullOrEmpty(BidderNo)) dict.Add("BidderNo", BidderNo);
            //if (!string.IsNullOrEmpty(SiteCollectionUrl)) dict.Add("DocumentLink", DocumentLink);

            if (TotalAmount != 0.0M) dict.Add("TotalAmount", TotalAmount.ToString(CultureInfo.InvariantCulture));
            if (Amount != 0.0M) dict.Add("Amount", Amount.ToString(CultureInfo.InvariantCulture));
            if (!StartTime.Equals(DateTime.MinValue)) dict.Add("StartTime", StartTime.ToString("yyyy-MM-ddTHH:mm:ssZ"));
            if (!ReceiptDate.Equals(DateTime.MinValue)) dict.Add("ReceiptDate", ReceiptDate.ToString("yyyy-MM-ddTHH:mm:ssZ"));

            return dict;
        }
    }
}